package Day9;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.junit.Test;

public class ExceotionDemo {
	@Test
	public void test1() {
		int i = 3;
		System.out.println(i/0);
	}
	
	@Test
	public void test2() {
		//编译时异常 写代码的时候不处理这个异常程序就直接报错无法编译
		try {
			//有可能抛出异常的代码
		} catch (Exception e) {
			// 如果代码抛出异常就会跑到catch里面
		}
		
		
		try {
			FileInputStream fileInputStream = new FileInputStream("");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void test3() {
		int num1 = 3;
		int num2 = 1;
		System.out.println("---start---");
		//由于可能会出现异常所以要用try吧可能出现异常的语句包含入内
		try {
			int result = num1 / num2;
		} catch (ArithmeticException e) {//如果代码抛出异常就会跑到catch里面
			// TODO: handle exception
		} finally {
			
			System.out.println("---finally---");
		}
		//因为catch捕获了异常所以程序没有挂掉而是继续往下执行
		System.out.println("---end---");
	}
	
	@Test
	public void test4() {
		try {
			FileInputStream fileInputStream = new FileInputStream("a.txt");
			fileInputStream.read();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	@Test
	public void test5() throws FileNotFoundException {
		FileInputStream fileInputStream = new FileInputStream("a.txt");
	}
	
	public static void main(String[] args) {
		try {
			fun();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//编译时异常可以不处理 throws抛出
	private static void fun() throws FileNotFoundException {
		// TODO Auto-generated method stub
		FileInputStream fileInputStream = new FileInputStream("a.txt");
	}
	
	public void fun2() throws FileNotFoundException {
		//这里做了一些非法的访问，这个文件找不到了的话可以主动抛出一个异常
		throw new FileNotFoundException("文件找不到了");
		
	}
	
	@Test
	public void test() {
		AccountManger accountManger = new AccountManger(100.0,1);//此时余额为100元，账号为1
		accountManger.doposite(100.0);//存入100元，此时余额为200元
		try {
			accountManger.withdraw(200.0);//取出200元，此时余额为0元
		} catch (MeiQianException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
